﻿
using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Threading.Tasks;

using FreeSql;
using Kalman;
using Newtonsoft.Json.Linq;

namespace QuDao
{
    /// <summary>
    /// 数据表sys_action_log关联的业务对象，处理该表相关的业务逻辑
    /// </summary>
    public sealed partial class ActionLogBO : SingletonBase<ActionLogBO>
    {
        readonly IFreeSql fsql;
        public ActionLogBO() : base()
        {
            fsql = ServiceLocator.Instance.GetRequiredService<IFreeSql>();
        }
        
		#region Generate Code

        /// <summary>
        /// 获取操作日志
        /// </summary>
        /// <param name="id">主键ID的值</param>
        /// <returns></returns>
        public T GetItem<T>(long id)
        {
        	if (id <= 0) return default;
            var item = fsql.Select<ActionLog>().Where(a => a.ID == id).ToOne<T>();
            return item;
        }
        
        public ActionLog GetItem(long id)
        {
        	return GetItem<ActionLog>(id);
        }
        
        /// <summary>
        /// 获取操作日志详情
        /// </summary>
        /// <param name="id">操作日志ID</param>
        /// <returns></returns>
        public ActionLogDetailsDto GetDetails(long id)
        {
            var dto = GetItem<ActionLogDetailsDto>(id);
            return dto;
        }

        /// <summary>
        /// 获取操作日志数据列表
        /// </summary>
        /// <param name="pageIndex">页索引，从1开始</param>
        /// <param name="pageSize">每页记录数，默认10</param>
        /// <returns></returns>
        public List<T> GetList<T>(int pageIndex = 1, int pageSize = 10)
        {
            var list = fsql.Select<ActionLog>().Page(pageIndex, pageSize).ToList<T>();
            return list;
        }
        
        /// <summary>
        /// 获取操作日志数据列表
        /// </summary>
        /// <returns></returns>
        public List<T> GetList<T>(long[] ids)
        {
        	if (ids == null || ids.Length == 0) return new List<T>();
            var list = fsql.Select<ActionLog>().Where(a => ids.Contains(a.ID)).ToList<T>();
            return list;
        }
        
        /// <summary>
        /// 新增操作日志
        /// </summary>
        public long Add(ActionLogAddDto dto)
        {
        	var entity = dto.CopyTo<ActionLog>();
        	
            entity.CreateTime = DateTime.Now;
            entity.ID = (long)fsql.Insert(entity).ExecuteIdentity();
            return entity.ID;
        }

		/// <summary>
        /// 删除操作日志数据
        /// </summary>
        /// <returns>返回影响行数</returns>
        public int Delete(long[] ids)
        {
        	if (ids == null || ids.Length == 0) return 0;
        	return fsql.Delete<ActionLog>().Where(a => ids.Contains(a.ID)).ExecuteAffrows();
        }
        
        /// <summary>
        /// 删除操作日志数据
        /// </summary>
        /// <returns>返回影响行数</returns>
        public int Delete(long id)
        {
        	return fsql.Delete<ActionLog>(id).ExecuteAffrows();
        }
        
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="req">分页查询请求对象</param>
        public PagingResult<T> Query<T>(PagingRequest req)
        {
        	if (req == null) throw new AppException("查询数据不能为空");
        	
            var select = fsql.Select<ActionLog>();
            foreach (var item in req.FilterItems)
            {
                switch (item.Field)
                {
                    //case "ActionLogID":
                    //    select.Where(a => a.ActionLogID == int.Parse(item.Value));
                    //    break;
                    //case "ActionLogName":
                    //    select.Where(a => a.ActionLogName.Contains(item.Value));
                    //    break;
                    default:
                        break;
                }
            }

            if (!string.IsNullOrEmpty(req.SortExp))
            {
                select.OrderBy(req.SortExp);//自定义排序
            }
            else
            {
                select.OrderByDescending(a => a.ID);//默认排序
            }

            var sql = select.ToSql();

            var list = select.Count(out var total).Page(req.PageIndex, req.PageSize).ToList<T>();
            return new PagingResult<T>
            {
                PageIndex = req.PageIndex,
                PageSize = req.PageSize,
                Data = list,
                Total = (int)total
            };
        }
        
        public PagingResult<ActionLog> Query(PagingRequest req)
        {
            return Query<ActionLog>(req);
        }
		#endregion
    }
}
